using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Filters;
using TMS.Application.Common.Interface;
using TMS.Domain.Entity;

namespace TMS.Infrastructrue.Filters
{
    public class HttpGlobalExceptionFilter: IAsyncExceptionFilter
    {
        private readonly IRepository<TMSExceptionLog> _TMSExceptionLogRes;

        public HttpGlobalExceptionFilter(IRepository<TMSExceptionLog> TMSExceptionLogRes)
        {
            _TMSExceptionLogRes = TMSExceptionLogRes;
        }


        public async Task OnExceptionAsync(ExceptionContext context)
        {
            //捕获全局异常，记入数据库日志
            await _TMSExceptionLogRes.AddAsync(new TMSExceptionLog
            {
                ShortMessage = context.Exception.Message,
                FullMessage = context.Exception.ToString()
            });

            context.ExceptionHandled = true;
        }
    }
}